<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" formType="add" labelWidth="100" :cols="3" :isSubmitBtn="false" @formChange="formChange" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog()">关 闭</el-button>
      <AjaxButton type="primary" :click="saveDialog">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { addWorkHourPrice, editWorkHourPrice } from '@/api/repair/repair/workHourPrice/index';
import { getChexSelectTable } from '@/api/repair/repairReception/customer/brand.js'; // 车系
export default {
  props: ['excloudSeries'],
  data() {
    return {
      formList: this.createFormList(),
      saveData: null
    };
  },
  mounted() {
    this.getChexSelect();
  },
  methods: {
    // 创建新增表单数据
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '工时单价代码',
          fieldName: 'labourPriceCode',
          placeholder: '请输入...',
          initialValue: '',
          style: { width: '150' },
          maxlength: 10,
          rules: [
            { required: true, message: '请输入正确的工时单价代码', trigger: 'blur' },
            { min: 0, max: 10, pattern: /^[a-zA-Z0-9]+$/, message: '请输入正确的工时单价代码' }
          ]
        },
        {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'labourPrice',
          placeholder: '请输入...',
          maxlength: 8,
          initialValue: '',
          style: { width: 'calc(99% - 40px)' },
          rules: [
            { required: true, message: '请输入正确的工时单价代码', trigger: 'blur' },
            { min: 0, max: 8, pattern: /(^\d+(\.)?(\d)?(\d)?$)/, message: '请输入正确的工时单价' }
          ],
          descOptions: {
            content: '元'
          }
        },
        {
          type: 'INPUT',
          label: '工时名称',
          fieldName: 'labourPriceName',
          placeholder: '请输入...',
          initialValue: '',
          maxlength: 50,
          style: { width: '150' },
          rules: [{ min: 0, max: 100, required: true, message: '请输入工时名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesCode',
          placeholder: '请选择...',
          itemList: [],
          style: { width: '150' },
          rules: [{ required: true, message: '请选择车系', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'isValid',
          placeholder: '请选择...',
          style: { width: '150' },
          initialValue: 10041001,
          itemList: [
            { value: 10041001, text: '启用' },
            { value: 10041002, text: '未启用' }
          ],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否在维修项目中启用',
          fieldName: 'isItemUse',
          placeholder: '请选择...',
          style: { width: '150' },
          initialValue: 10041002,
          itemList: [
            { value: 10041001, text: '启用' },
            { value: 10041002, text: '未启用' }
          ],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }],
          change: val => {
            if (val == 10041001) {
              this.$alert('启用工时单价定义将会重新计算并替换该车系下所有的维修项目的工时费，请确认！', '提示！', {
                confirmButtonText: '确定'
              });
            }
          }
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'labourRemark',
          placeholder: '请输入...',
          initialValue: '',
          selfCols: 2,
          style: { width: '150' },
          rules: [{ message: '请输入备注', trigger: 'blur' }]
        }
      ];
    },

    checkPrice(rule, value, callback) {
      if (!value) {
        return callback(new Error(rule.message));
      } else {
        const reg = /^[0-9a-zA-Z]+$/;
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error(rule.message));
        }
      }
      callback();
    },
    // 表单提交触发事件
    formChange(saveData) {},
    async saveDialog() {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      this.saveData = data;
      console.log(err);
      console.log(data);
      if (err) return;

      let res = await addWorkHourPrice(this.saveData);
      console.log(res);
      if (res.resultCode === 200) {
        this.closeDialog('refresh');
      } else {
        // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
        this.closeDialog();
      }

      // else {
      //   return this.$notify.warning({ title: '提示信息', message: '有未校验通过的信息' });
      // }
    },

    closeDialog(param) {
      this.$emit('close', param);
    },

    async getChexSelect() {
      // let queryParams = { isSeriesId: '1' };
      let projectModelList = (await getChexSelectTable()).data.map(x => ({ value: x.seriesCode + '', text: x.seriesName }));
      let that = this;
      const result = projectModelList.filter(function(v) {
        return !that.excloudSeries.some(val => {
          return val.SERIES_CODE == v.value;
        });
      });
      this.formList.find(x => x.fieldName === 'seriesCode').itemList = result;
    }
  }
};
</script>

<style></style>
